'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

from carcomp.framework import component
from carcomp.apps.gamma.util import bitmap, small_hash_map
from carcomp.apps.gamma import constants

class Bloom(component.Implementation):
    '''
    classdocs
    '''


    def __init__(self, name):
        '''
        Constructor
        '''
        component.Implementation.__init__(self, name)
        self.add_port(component.Port('A', 'set_bit_map'))
        self.add_port(component.Port('sendA', '__OUT__'))
        self.add_port(component.Port('sendMap', '__OUT__'))
        self._bm = bitmap.BitMap(constants.SIZE)
        
    def set_bit_map(self, msg):
        '''
        Create bitmap from tuples
        '''
        if msg != 'DONE':
            tuple = msg.split(':')
            index = small_hash_map.get_key(constants.SIZE, tuple[constants.JOIN_KEYA])
            self._bm.set(index)
            self.send_out('sendA', msg)
        else:
            self.send_out('sendMap', self._bm.serialize())
            self.send_out('sendA', msg)
            
class BFilter(component.Implementation):
    
    Debug = False
    
    def __init__(self, name):
        component.Implementation.__init__(self, name)
        self.add_port(component.Port('B', 'read_b'))
        self.add_port(component.Port('BM', 'read_bm'))
        self.add_port(component.Port('BPrime', '__OUT__'))
        self._b_list = []
        self._got_bm = False
        self._bm = None
        
    def read_b(self, msg):
        if BFilter.Debug:
            print('%s rcvd: %s'%(self._name, msg.strip()))
        if self._got_bm is False:
            if msg != 'FAKE':
                self._b_list.append(msg)
        else:
            if msg != 'FAKE':
                self._b_list.append(msg)
            for tuplestr in self._b_list:
                tuple = tuplestr.split(':')
                if self._bm.isSet(small_hash_map.get_key(constants.SIZE, tuple[constants.JOIN_KEYB])):
                    if BFilter.Debug:
                        print('%s sending: %s'%(self._name,tuplestr))
                    self.send_out('BPrime', tuplestr)
            self._b_list = []
    
    def read_bm(self, msg):
        self._bm = bitmap.BitMap(constants.SIZE, msg)
        self._got_bm = True
        if BFilter.Debug:
            print('%s got bm, switching'%self._name)
        self.read_b('FAKE')
            
        